# -*- coding: utf-8; -*-
import datetime as DT

from odoo.tools.translate import _
from odoo import tools
from odoo import models, api


class GreeProcurentTransientWizard(models.TransientModel):
    _name = 'gree.procurement.transient.wizard'

    @api.multi
    def transform(self):
        transient_obj = self.env['gree.procurement.transient']
        procurement_obj = self.env['gree.procurement']
        item_obj = self.env['gree.procurement.item']
        procurement = procurement_obj.create({})
        for p in transient_obj.search([]):
            date = DT.datetime.strptime(p.date + ' ' + p.time, '%Y-%m-%d %H:%M').strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
            item_obj.create({'procurement_id': procurement.id,
                             'date': date,
                             'product_code': p.product_code,
                             'product_name': p.product_name,
                             'product_design_id': p.product_design_id,
                             'product_model': p.product_model,
                             'mrp_order': p.mrp_order,
                             'is_spare_part': p.is_spare_part,
                             'qty': p.qty,
                             'available_qty': p.available_qty,
                             'woc': p.woc,
                             'sequence': p.sequence,
                             'purchaser': p.purchaser,
                             'supplier_id': p.supplier_id,
                             'supplier_name': p.supplier_name,
                             'roh': p.roh,
                             'mrp_order_start_at': p.mrp_order_start_at,
                             'note': p.note
            })
            p.unlink()
        return {
            # 'domain': "[('id','in', [" + procurement.id + "])]",
            'name': u"需求单",
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'gree.procurement',
            'view_id': False,
            'res_id': procurement.id,
            'type': 'ir.actions.act_window'
        }

